package com.jason.sss.controller;

import com.jason.sss.constant.ResponseEnum;
import com.jason.sss.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/")
@Controller
public class LoginController {

    @RequestMapping("/user/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/user/doLogin")
    @ResponseBody
    public Result doLogin(String userName, String password, HttpServletRequest request) {
        System.out.println("username: " + userName + "   password: " + password);
        if (userName == null || userName.isEmpty())
            return new Result(ResponseEnum.EMPTY_USERNAME);
        if (password == null || password.isEmpty())
            return new Result(ResponseEnum.EMPTY_PASSWORD);
        if ("admin".equals(userName) && "admin".equals(password)) {
            request.getSession().setAttribute("userInfo", "admin");
            return new Result(ResponseEnum.SUCCESS);
        } else {
            return new Result(ResponseEnum.LOGIN_ERROR);
        }
    }
}
